import axios from "axios";
import transform from "../utils/transform";

export default {
  data() {
    return {
      tableData: [],
      pageSize: 10,
      pageIndex: 1,
      count: 0,
      loading: false,
      dialogVisible: false,
      dialogVisible1: false,
      opId: 0
    };
  },
  methods: {
    async initData() {
      if (this.loading) {
        return;
      }
      this.loading = true;
      const {
        data: { errno, message, data }
      } = await axios.get(this.getUrl, {
        params: transform({
          pageSize: this.pageSize,
          pageIndex: this.pageIndex - 1,
          ...this.searchForm
        })
      });
      if (!errno) {
        this.tableData = data.data;
        this.count = data.count;
      } else {
        this.$message({ type: "error", message });
      }
      this.loading = false;
    },
    currentChange(page) {
      this.pageIndex = page;
      this.initData();
    },
    sizeChange(size) {
      this.pageIndex = 1;
      this.pageSize = size;
      this.initData();
    },
    onSearch() {
      this.pageIndex = 1;
      this.initData();
    },
    reset() {
      for (const key in this.searchForm) {
        if (this.searchForm[key] !== "") {
          this.searchForm[key] = "";
        }
      }
      this.onSearch();
    },
    toRemove(id) {
      this.$confirm("是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        const {
          data: { errno, message }
        } = await axios.delete(this.delUrl || this.getUrl + "/" + id);
        if (!errno) {
          this.$message({ type: "success", message: "操作成功" });
          this.onSearch();
        } else {
          this.$message({ type: "error", message });
        }
      });
    },
    toPushOn(id) {
      this.$confirm("是否上架?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        const {
          data: { errno, message }
        } = await axios.patch(this.getUrl + "/status", {
          id,
          status: 2
        });
        if (!errno) {
          this.$message({ type: "success", message: "操作成功" });
          this.onSearch();
        } else {
          this.$message({ type: "error", message });
        }
      });
    },
    toPushOff(id) {
      this.$confirm("是否下架?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        const {
          data: { errno, message }
        } = await axios.patch(this.getUrl + "/status", {
          id,
          status: 1
        });
        if (!errno) {
          this.$message({ type: "success", message: "操作成功" });
          this.onSearch();
        } else {
          this.$message({ type: "error", message });
        }
      });
    },
    toAdd() {
      this.opId = 0;
      this.dialogVisible = true;
    },
    toEdit(id) {
      this.opId = id;
      this.dialogVisible = true;
    },
    toDetail(id) {
      this.opId = id;
      this.dialogVisible1 = true;
    },
    cancel() {
      this.dialogVisible = false;
      this.initData();
    },
    cancel1() {
      this.dialogVisible1 = false;
    }
  },
  mounted() {
    this.initData();
  }
};
